/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GamePlay;

import ClassFrame.Button;
import ClassFrame.Image;
import ClassFrame.InputKey;
import ClassFrame.Map;
import ClassFrame.Player;
import ClassFrame.RMS;
import ClassFrame.Resource;
import ClassFrame.XMLParser;
import Object.Question;
import Screen.IScreen;
import Screen.Menu;
import Screen.Splash;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;

/**
 *
 * @author QuyetNM1
 */
public class CanvasGame extends Canvas implements Runnable {

    MIDletGame midletGame;
    public static final Font font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE);
    public static final Font bmFont = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_MEDIUM);
    public static final Font blFont = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE);
    public static final int WHITE = 0xFFFFFF;
    public static final int BLACK = 0x000000;
    public static final int RED = 0xFF0000;
    public static int MAX_CPS = 20;
    public static int MS_PER_FRAME = 1000 / MAX_CPS;
    public static boolean running;
    public static boolean active;
    //sound properties
    public static boolean vibration = false;
    public static final int wScreen = 240;
    public static final int hScreen = 320;
    //game Screen
    public static final int NONE = 0;
    public static final int SCR_SPLASH = 1;
    public static final int SCR_MENU = 2;
    public static final int SCR_SCORE = 3;
    public static final int SCR_SETTING = 4;
    public static final int SCR_GUIDE = 5;
    public static final int SCR_ABOUT = 6;
    public static final int SCR_EXIT = 7;
    public static final int SCR_GAME = 8;
    public static final int SCR_PAUSE = 9;
    public static final int SCR_NEXT = 10;
    public static final int SCR_WIN = 11;
    public static final int SCR_GAMEOVER = 12;
    public static int score_temp;
    public static int level_temp;
    private static int typeScreen;
    public Resource res;
    private IScreen screen;
    public static RMS rms;
    private Image image;
    private int continuePlay = NONE;
    public static Question[] qList;
    public static int musicLevel = 80;
    public static boolean isSound = true;
    XMLParser xmlParser;
    Map map;
    Player aPlayer = new Player(this, 0, 0, 0, 0, ""); 
    ///////////////////////
    public CanvasGame(MIDletGame midletGame) {
        Runtime.getRuntime().gc();
        this.setFullScreenMode(true);
        this.midletGame = midletGame;
        initCanvas();
        start();
//        AudioManager.playEffects();
    }

    private void initCanvas() {
        initComponents();
        active = true;
        running = true;

        image = new Image();
        rms = new RMS(this);
        rms.open();

        Player p;
        p = rms.read(6);
        if (p.getLevel() > 0) {
            continuePlay = -5;
            level_temp = p.getLevel();
            score_temp = p.getScore();
        }
        rms.close();
        xmlParser = new XMLParser();
        qList = xmlParser.getQuestionList();
        map = new Map(this);
        
        rms.open();
        Map.levelMax = rms.readLevelMax();
        System.out.println("level max " + Map.levelMax);
        rms.close();
    }

    private void initComponents() {
        res = new Resource(this);
        res.loadSounds();
        if (screen == null) {
            screen = new Splash(this);
        }

        screen.load_screen();
    }

    public void keyEvent(int keycode, int event) {
        screen.keyEvent(keycode, event);
    }

    public void pointerEvent(int x, int y, int event) {
        screen.pointerEvent(x, y, event);
    }

    protected void paint(Graphics g) {
        screen.paint(g);
    }

    private void update() {
    }

    
    boolean playSoundAgain = true;
    
    protected void hideNotify() {
        if(Menu.isTB) return;
        playSoundAgain = true;
        if(!Splash.isSplash){
            try {
                if(Resource.iSound >= 0){
                    this.getResource().getSound(Resource.iSound).stop();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        screen.un_load_screen();
    }
    protected void showNotify() {
        playSoundAgain = false;
        if(Menu.isTB) return;
        if(!Splash.isSplash){
            try {
                if(Resource.iSound >= 0){
                    this.getResource().getSound(Resource.iSound).start();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        screen.load_screen();
    }


    
    public void run() {
        while (running) {
            long cycleStartTime = System.currentTimeMillis();
            long timeSinceStart = 0;
            if (active) {
                repaint();
                timeSinceStart = System.currentTimeMillis() - cycleStartTime;
            }
            try {
                if (timeSinceStart < 1000 / MAX_CPS) {
                    Thread.sleep(1000 / MAX_CPS - timeSinceStart);
                };
            } catch (InterruptedException ex) {
                //sendError("");
            }
        }
        midletGame.destroyApp(true);
        midletGame = null;
        Runtime.getRuntime().gc();
    }

    public void freeComponents() {
        res.unLoadSounds();
        res = null;
        screen = null;
    }

    public final void start() {
        Thread runner = new Thread(this);
        runner.setPriority(Thread.MAX_PRIORITY);
        runner.start();
    }

    // Catch key-down input and save in to Input Array
    protected void keyPressed(int keyCode) {
        keyEvent(keyCode, InputKey.KEY_EVENT_DOWN);
    }

    // Catch key-up input and clear from Input Array
    protected void keyReleased(int keyCode) {
        keyEvent(keyCode, InputKey.KEY_EVENT_UP);
    }

    protected void pointerPressed(int x, int y) {
        pointerEvent(x, y, InputKey.POINTER_EVENT_DOWN);
    }

    protected void pointerReleased(int x, int y) {
        pointerEvent(x, y, InputKey.POINTER_EVENT_UP);
    }

    protected void pointerDragged(int x, int y) {
        pointerEvent(x, y, InputKey.POINTER_EVENT_DRAG);
    }

    public boolean checkRegion(int point_x, int point_y, int region_X, int region_Y, int region_W, int region_H) {
        if (point_x < region_X || point_x > region_X + region_W) {
            return false;
        } else if (point_y < region_Y || point_y > region_Y + region_H) {
            return false;
        } else {
            return true;
        }
    }

    public boolean checkButton(int point_x, int point_y, Button aButton) {
        if (aButton.getState() == Button.DISABLE) {
            return false;
        }
        if (point_x <= aButton.getX() || point_x >= aButton.getX() + aButton.getW()) {
            return false;
        } else if (point_y <= aButton.getY() || point_y >= aButton.getY() + aButton.getH()) {
            return false;
        } else {
            return true;
        }
    }

    public void setScreen(final IScreen screen) {
        try {
            this.screen.un_load_screen();
            this.screen = screen;
            this.screen.load_screen();
            this.screen.initComponents();
        } catch (NullPointerException npe) {
            System.out.println("screen is null");
            System.out.println(npe.getMessage());
        }
    }

    public Resource getResource() {
        return this.res;
    }

    public Image getImage() {
        return this.image;
    }

    public RMS getRMS() {
        return this.rms;
    }

    public MIDletGame getMIDletGame() {
        return this.midletGame;
    }

    public IScreen getScreen() {
        return this.screen;
    }

    public int getContinuePlay() {
        return this.continuePlay;
    }

    public void setContinuePlay(int continuePlay) {
        this.continuePlay = continuePlay;
    }

    public static int getTypeScreen() {
        return typeScreen;
    }

    public static void setTypeScreen(int typeScreen) {
        CanvasGame.typeScreen = typeScreen;
    }

    public Map getMap() {
        return this.map;
    }

    public Player getaPlayer() {
        return aPlayer;
    }

    public void setaPlayer(Player aPlayer) {
        this.aPlayer = aPlayer;
    }
    
}
